package com.hellowx.controller;

import com.hellowx.entity.Product;
import com.hellowx.vo.ProductVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 *
 * @author MagicMushroom
 * @date 2021/7/4
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Value("${server.port}")
    private String port;

    public static final Logger logger = LoggerFactory.getLogger(ProductController.class);

    @GetMapping
    public Map<String, Object> product(HttpServletRequest request){
        logger.info("product ok");
//        return "product ok---devtools ok"+port;
//        return request.getHeader("User-Name")+"啦啦啦哈哈哈哈";
        Map<String, Object> map=new HashMap<>();
        map.put("requestHeader:User-Name",request.getHeader("User-Name"));
        map.put("requestParam:","color:"+request.getParameter("color"));
        return map;
    }

    @GetMapping("/testQueryString")
    public String testQueryString(Integer id,String name){
        logger.info("id:{}  name:{}",id,name);
        return "testQueryString ok !!!:"+port;
    }

    @GetMapping("/testRestFull/{id}/{name}")
    public String testRestFull(@PathVariable("id")Integer id,@PathVariable("name") String name){
        logger.info("id:{}  name:{}",id,name);
        return "testRestFull ok!!!:"+port;
    }

    @PostMapping("/testJson")
    public String testJson(@RequestBody Product product){
        logger.info("id:{}  name:{}",product.getId(),product.getName());
        return "testJson ok!!!:"+port;
    }

    @GetMapping("/testArray")
    public String testArray(Integer[] ids){
        for (Integer id : ids) {
            logger.info("id:{}",id);
        }
        return "testArray ok!!!:"+port;
    }

    @GetMapping("testList")
    public String testList(ProductVO productVO){
        for (Integer id : productVO.getIds()) {
            logger.info("id:{}",id);
        }
        return "testList ok!!!:"+port;
    }

    @GetMapping("/testObject")
    public String testObject(Product product){
        logger.info("product:{}",product.toString());
        return "testObject ok!!!:"+port;
    }

    @GetMapping("/{id}")
    public Product testReturnObject(@PathVariable("id") Integer id){
        Product product = new Product();
        product.setId(id);
        product.setName("aa");
        return product;
    }

    @GetMapping("/find/{categoryId}")
    public List<Product> testReturnListObject(@PathVariable("categoryId")Integer categoryId){
        List<Product> productList=  new ArrayList<>();

        productList.add(new Product(1,"A",categoryId));
        productList.add(new Product(2,"B",categoryId));
        productList.add(new Product(3,"C",categoryId));

        return productList;
    }

    @GetMapping("findAll")
    public Map<String, Object> testReturnMap(Integer pageIndex,Integer pageSize){
        Integer total = 1000;
        Map<String, Object> map = new HashMap<>();
        map.put("total",total);
        List<Product> productList=  new ArrayList<>();

        productList.add(new Product(1,"A",1));
        productList.add(new Product(2,"B",2));
        productList.add(new Product(3,"C",5));
        map.put("list",productList);
        return map;
    }

}
